<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商品id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入商品id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="commodityname">
        <el-input
          v-model="queryParams.commodityname"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属店铺" prop="shop">
        <el-input
          v-model="queryParams.shop"
          placeholder="请输入所属店铺"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="预估单量" prop="estimatedsinglequantity">
        <el-input
          v-model="queryParams.estimatedsinglequantity"
          placeholder="请输入预估单量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算费用" prop="settlementcost">
        <el-input
          v-model="queryParams.settlementcost"
          placeholder="请输入结算费用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="分配人id" prop="distributorid">
        <el-input
          v-model="queryParams.distributorid"
          placeholder="请输入分配人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属用户" prop="owninguser">
        <el-input
          v-model="queryParams.owninguser"
          placeholder="请输入所属用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属机构" prop="organization">
        <el-input
          v-model="queryParams.organization"
          placeholder="请输入所属机构"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
      <el-radio-group v-model="radio3" size="small">
        <el-radio-button label="全部"></el-radio-button>
        <el-radio-button label="今天"></el-radio-button>
        <el-radio-button label="昨天"></el-radio-button>
        <el-radio-button label="本周"></el-radio-button>
        <el-radio-button label="本月"></el-radio-button>
        <el-radio-button label="上月"></el-radio-button>
      </el-radio-group>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mine:mine:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mine:mine:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mine:mine:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mine:mine:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="mineList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品名称" align="center" prop="commodityname" />
      <el-table-column label="所属店铺" align="center" prop="shop" />
      <el-table-column label="商品类目" align="center" prop="category" />
      <el-table-column label="商品地址" align="center" prop="address" />
      <el-table-column label="优惠券地址" align="center" prop="coupon" />
      <el-table-column label="优惠券来源" align="center" prop="couponid" />
      <el-table-column label="商品图片" align="center" prop="picture" />
      <el-table-column
        label="活动开始时间"
        align="center"
        prop="activitytimea"
      />
      <el-table-column
        label="活动结束时间"
        align="center"
        prop="activitytimeb"
      />
      <el-table-column label="佣金比例" align="center" prop="commissionratio" />
      <el-table-column label="服务费" align="center" prop="servicecharge" />
      <el-table-column label="合作模式" align="center" prop="cooperativemode" />
      <el-table-column label="库存" align="center" prop="inventory" />
      <el-table-column label="是否包邮" align="center" prop="freemailornot" />
      <el-table-column
        label="推荐理由"
        align="center"
        prop="recommendationreason"
      />
      <el-table-column
        label="1开始2进行3结束"
        align="center"
        prop="thirdstage"
      />
      <el-table-column
        label="预估服务费"
        align="center"
        prop="estimatedservicecharge"
      />
      <el-table-column
        label="预估单量"
        align="center"
        prop="estimatedsinglequantity"
      />
      <el-table-column label="结算费用" align="center" prop="settlementcost" />
      <el-table-column
        label="所属平台1天猫2京东"
        align="center"
        prop="platform"
      />
      <el-table-column label="商品类型1内部2外部" align="center" prop="type" />
      <el-table-column label="分配人id" align="center" prop="distributorid" />
      <el-table-column label="渠道id" align="center" prop="channelid" />
      <el-table-column label="所属用户" align="center" prop="owninguser" />
      <el-table-column label="所属机构" align="center" prop="organization" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mine:mine:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mine:mine:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属平台" prop="field101">
          <el-select
            v-model="form.platform"
            placeholder="请选择所属平台"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in field101Options"
              :key="index"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品地址" prop="address">
          <!-- <el-input v-model="form.address" placeholder="请输入商品地址" > -->
          
          <!-- </el-input> -->
          <el-input placeholder="请输入内容" v-model="form.address" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="getmsg()"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="优惠券" prop="coupon" >
          <el-input v-model="form.coupon" placeholder="请输入优惠券地址" />
        </el-form-item>
        <el-form-item label="商品id" prop="id">
          <el-input v-model="form.id" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="商品名称" prop="commodityname">
          <el-input v-model="form.commodityname" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="所属店铺" prop="shop">
          <el-input v-model="form.shop" placeholder="请输入所属店铺" />
        </el-form-item>
        <el-form-item label="商品类目" prop="category">
          <el-input v-model="form.category" placeholder="请输入商品类目" />
        </el-form-item>
        <el-form-item label="优惠券ID" prop="couponid">
          <el-input v-model="form.couponid" placeholder="请输入优惠券ID" />
        </el-form-item>
        <el-form-item label="商品图片" prop="picture">
          <el-input v-model="form.picture" placeholder="请输入商品图片" />
        </el-form-item>
        <el-form-item label="活动开始时间" prop="activitytimea">
          <el-input
            v-model="form.activitytimea"
            placeholder="请输入活动开始时间"
          />
        </el-form-item>
        <el-form-item label="活动结束时间" prop="activitytimeb">
          <el-input
            v-model="form.activitytimeb"
            placeholder="请输入活动结束时间"
          />
        </el-form-item>
        <el-form-item label="佣金比例" prop="commissionratio">
          <el-input
            v-model="form.commissionratio"
            placeholder="请输入佣金比例"
          />
        </el-form-item>
        <el-form-item label="服务费" prop="servicecharge">
          <el-input v-model="form.servicecharge" placeholder="请输入服务费" />
        </el-form-item>
        <el-form-item label="合作模式" prop="cooperativemode">
          <el-input
            v-model="form.cooperativemode"
            placeholder="请输入合作模式"
          />
        </el-form-item>
        <el-form-item label="库存" prop="inventory">
          <el-input v-model="form.inventory" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="是否包邮" prop="freemailornot">
          <el-input v-model="form.freemailornot" placeholder="请输入是否包邮" />
        </el-form-item>
        <el-form-item label="推荐理由" prop="recommendationreason">
          <el-input
            v-model="form.recommendationreason"
            placeholder="请输入推荐理由"
          />
        </el-form-item>
        <el-form-item label="1开始2进行3结束" prop="thirdstage">
          <el-input
            v-model="form.thirdstage"
            placeholder="请输入1开始2进行3结束"
          />
        </el-form-item>
        <el-form-item label="预估服务费" prop="estimatedservicecharge">
          <el-input
            v-model="form.estimatedservicecharge"
            placeholder="请输入预估服务费"
          />
        </el-form-item>
        <el-form-item label="预估单量" prop="estimatedsinglequantity">
          <el-input
            v-model="form.estimatedsinglequantity"
            placeholder="请输入预估单量"
          />
        </el-form-item>
        <el-form-item label="结算费用" prop="settlementcost">
          <el-input
            v-model="form.settlementcost"
            placeholder="请输入结算费用"
          />
        </el-form-item>
        <el-form-item label="分配人id" prop="distributorid">
          <el-input v-model="form.distributorid" placeholder="请输入分配人id" />
        </el-form-item>
        <el-form-item label="渠道id" prop="channelid">
          <el-input v-model="form.channelid" placeholder="请输入渠道id" />
        </el-form-item>
        <el-form-item label="所属用户" prop="owninguser">
          <el-input v-model="form.owninguser" placeholder="请输入所属用户" />
        </el-form-item>
        <el-form-item label="所属机构" prop="organization">
          <el-input v-model="form.organization" placeholder="请输入所属机构" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMine,
  getMine,
  delMine,
  addMine,
  updateMine,
} from "@/api/mine/mine";

export default {
  name: "Mine",
  data() {
    return {
      radio3: "全部",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      mineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        commodityname: null,
        shop: null,
        thirdstage: null,
        estimatedservicecharge: null,
        estimatedsinglequantity: null,
        settlementcost: null,
        platform: null,
        type: null,
        distributorid: null,
        owninguser: null,
        organization: null,
      },
      formData: {
        field101: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        field101: [
          {
            required: true,
            message: "请择选商品平台",
            trigger: "blur",
          },
        ],
        id: [{ required: true, message: "商品id不能为空", trigger: "blur" }],
        commodityname: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
        shop: [
          { required: true, message: "所属店铺不能为空", trigger: "blur" },
        ],
        category: [
          { required: true, message: "商品类目不能为空", trigger: "blur" },
        ],
        address: [
          { required: true, message: "商品地址不能为空", trigger: "blur" },
        ],
        coupon: [
          { required: true, message: "优惠券地址不能为空", trigger: "blur" },
        ],
        couponid: [
          { required: true, message: "优惠券来源不能为空", trigger: "blur" },
        ],
        picture: [
          { required: true, message: "商品图片不能为空", trigger: "blur" },
        ],
        activitytimeb: [
          { required: true, message: "活动结束时间不能为空", trigger: "blur" },
        ],
        commissionratio: [
          { required: true, message: "佣金比例不能为空", trigger: "blur" },
        ],
        servicecharge: [
          { required: true, message: "服务费不能为空", trigger: "blur" },
        ],
        cooperativemode: [
          { required: true, message: "合作模式不能为空", trigger: "blur" },
        ],
        inventory: [
          { required: true, message: "库存不能为空", trigger: "blur" },
        ],
        freemailornot: [
          { required: true, message: "是否包邮不能为空", trigger: "blur" },
        ],
        recommendationreason: [
          { required: true, message: "推荐理由不能为空", trigger: "blur" },
        ],
        thirdstage: [
          {
            required: true,
            message: "1开始2进行3结束不能为空",
            trigger: "blur",
          },
        ],
        estimatedservicecharge: [
          { required: true, message: "预估服务费不能为空", trigger: "blur" },
        ],
        estimatedsinglequantity: [
          { required: true, message: "预估单量不能为空", trigger: "blur" },
        ],
        settlementcost: [
          { required: true, message: "结算费用不能为空", trigger: "blur" },
        ],
        platform: [
          {
            required: true,
            message: "所属平台1天猫2京东不能为空",
            trigger: "blur",
          },
        ],
        type: [
          {
            required: true,
            message: "商品类型1内部2外部不能为空",
            trigger: "change",
          },
        ],
        owninguser: [
          { required: true, message: "所属用户不能为空", trigger: "blur" },
        ],
        organization: [
          { required: true, message: "所属机构不能为空", trigger: "blur" },
        ],
      },
      field101Options: [
        {
          label: "京东",
          value: 1,
        },
        {
          label: "天猫",
          value: 2,
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listMine(this.queryParams).then((response) => {
        this.mineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        commodityname: null,
        shop: null,
        category: null,
        address: null,
        coupon: null,
        couponid: null,
        picture: null,
        activitytimea: null,
        activitytimeb: null,
        commissionratio: null,
        servicecharge: null,
        cooperativemode: null,
        inventory: null,
        freemailornot: null,
        recommendationreason: null,
        thirdstage: null,
        estimatedservicecharge: null,
        estimatedsinglequantity: null,
        settlementcost: null,
        platform: null,
        type: null,
        distributorid: null,
        channelid: null,
        owninguser: null,
        organization: null,
      };
      this.resetForm("form");
    },
    // 一个获取数据的方法
    getmsg(){

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMine(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // console.log(this.$refs["form"]);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateMine(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMine(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除商品编号为"' + ids + '"的数据项？')
        .then(function () {
          return delMine(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mine/mine/export",
        {
          ...this.queryParams,
        },
        `mine_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
